package com.jsmtr.rms.controller;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IDoSZService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


//实习
@Controller
@RequestMapping("/Do/SZPractice")
public class DoSZPracticeController {

    @Resource
    private IUtilService utilService;
    @Resource
    private IDoSZService szService;
    @Resource
    private IDoResumeService doResumeService;
    @Resource
    private IProjectService projectService;


    //[start] 查询毕业生信息

    //首页
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();
            String deptcode = request.getParameter("deptcode").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);

            //名族
            model.addAttribute("minzu", utilService.optionList("B", "0"));
            //婚否
            model.addAttribute("hunfou", utilService.optionList("D", "0"));
            //政治面貌
            model.addAttribute("zzmm", utilService.optionList("E", "0"));
            //最高学历
            model.addAttribute("education", utilService.optionList("F", "0"));
            //专业技术职务类型
            model.addAttribute("posttype", utilService.optionList("I", "0"));
            //职业资格证
            model.addAttribute("jndj", utilService.optionList("O", "0"));

            //招聘岗位
            List<ProjectInfoJobs> projectJobs = utilService.getProjectJobs(pid);
            model.addAttribute("jobs", projectJobs);

            List<BaseConfig> configs = utilService.getConfigs("dzmb");
            StringBuilder str1 = new StringBuilder();
            for (BaseConfig config : configs) {
                str1.append("<option value='" + config.getCont() + "'>" + config.getCont() + "</option>");
            }
            model.addAttribute("address", str1);

            List<BaseConfig> _configs = utilService.getConfigs("dxmb");
            StringBuilder str3 = new StringBuilder();
            for (BaseConfig _config : _configs) {
                str3.append("<option value='" + _config.getCode() + "'>" + _config.getName() + "</option>");
            }
            model.addAttribute("dxmb", str3);

            //实习注意事项
           /* String sxzysx=utilService.getConfig("sxzysx");
            model.addAttribute("sxzysx",sxzysx);*/

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/SZ/Practice/index";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();
            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom") == null ? "" : request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto") == null ? "" : request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom") == null ? "" : request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto") == null ? "" : request.getParameter("heightto").trim());
            _map.put("rcount", request.getParameter("graduationyear") == null ? "" : request.getParameter("graduationyear").trim());
            //Map<String, Object> map = recommendService.getUsers(page, perpage, user,_map);
            Map<String, Object> map = szService.getResumesMap(page, perpage, user, _map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");
            if (pageCount != 0) {

                int num = 0;
                // Object object = map.get("list");
                //  for (HRZZUser _user : (List<HRZZUser>) object) {
                List<HashMap> object = (List<HashMap>) map.get("list");
                for (Map<String, Object> _user : object) {
                    result.append("<tr><td><input type=\"checkbox\" name='uid' value='" + _user.get("ID") + "' id='cb_" + _user.get("ID") + "' /></td>");
                    result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + _user.get("ID") + "'>" + _user.get("NAME") + "</a></td>");
                    result.append("<td>" + _user.get("SEXNAME") + "</td>");
                    result.append("<td>" + _user.get("CARDNO") + "</td>");
                    result.append("<td>" + _user.get("WORKEXPERIENCE") + "年</td>");
                    result.append("<td>" + _user.get("JNAME") + "</td>");
                    result.append("<td>" + _user.get("MAJORNAME") + "</td>");
                    result.append("<td>" + _user.get("EDUCATIONNAME") + "</td>");
                    result.append("<td>" + _user.get("TEL") + "</td>");
                    result.append("<td>" + (_user.get("SXSTATE") == null ? "" : _user.get("SXSTATE")) + "</td>");
                    result.append("<td>" + (_user.get("SXREMARK") == null ? "" : _user.get("SXREMARK")) + "</td>");
                   /* result.append("<td><div><select name='user[" + num + "].sxstate'>");
                    result.append("<option value='未安排'>未安排</option><option value='已安排'>已安排</option><option value='未通过'>未通过</option><option value='已通过'>已通过</option>");
                    result.append("</select><input type='hidden' value='" + _user.getSxstate() + "' /></div></td>")*/
                    ;
                    if (_user.get("OP7") == null || _user.get("OP7").equals("未公布")) {
                        result.append("<td>" + (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td>" + "最近登录：" + (_user.get("LOGINTIME")==null?"":_user.get("LOGINTIME").toString()) + "<br/>个人情况：" + (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='false'><span>已公布</span></td>");
                    }
                    if (_user.get("FQSTATE").equals("已放弃") && _user.get("FQFID").equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (_user.get("SXSTATE").equals("已通过") || (_user.get("SXSTATE").equals("未安排") && !_user.get("FID").equals(flowid))) {
                            result.append("<td class='false'><span>已完成</span></td></tr>");
                        } else {
                            result.append("<td class='true'><span>未完成</span></td></tr>");
                        }
                    }

                    num++;
                }
            } else {
                result.append("<tr style='display:none'><td></td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='12'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }
    //[end]

    //[start] 提交实习结果

    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping(value = "/RAction", method = RequestMethod.POST)
    public void SaveAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();//招聘方案ID
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            String[] uids = request.getParameterValues("uid");
            if (uids.length > 0) {
                for (int i = 0; i < uids.length; i++) {
                    uids[i] = uids[i].replaceAll("cb_", "");
                }
            }

            String sxstate = request.getParameter("sxstate");
            String sxremark = request.getParameter("sxremark");
            if (uids == null) {
                utilService.printJsonResult(false, "请选择人员！", "no", response);
            } else {
                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        Date time = new Date();
                        List editlist = new ArrayList();
                   /* for (HRZZUser user : users.getUser()) {
                        HRZZUser usr = recommendService.getViewUser(user.getId());*/
                        List<DUserResume> users = szService.getResumes(uids);
                        for (DUserResume user : users) {
                            if (user.getFqstate().equals("未放弃") && user.getFid().equals(fid) && user.getNrid() == null) {
                                user.setSxdate(new Date());
                                user.setSxremark(sxremark);
                                user.setSxstate(sxstate);
                                user.setSxuid(cuser.getId());
                                user.setSxuname(cuser.getUname());
                                editlist.add(user);
                            }
                        }
                        if (editlist.size() > 0) {
                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前信息不能执行此操作！", "no", response);
                        }

                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 流程扭转到下一步

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    List editlist = new ArrayList();
                    List<DUserResume> users = szService.getResumes(ids);
                    ProjectInfoFlow nFlow = szService.getNextFlow(pid, deptcode, fid);
                    for (DUserResume user : users) {
                        if (user.getFid().equals(fid) && user.getFqstate().equals("未放弃")&& user.getNrid() == null) {
                            if( user.getSxstate().equalsIgnoreCase("通过") || user.getSxstate().equalsIgnoreCase("已通过")||user.getSxstate().equalsIgnoreCase("合格")||user.getSxstate().equalsIgnoreCase("已合格")){
                                user.setFid(nFlow.getId());
                                user.setPath(user.getPath() + nFlow.getId() + "/");
                                editlist.add(user);
                            }

                        }
                    }

                    if (editlist.size() > 0) {
                        nFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                        editlist.add(nFlow);

                        utilService.editList(editlist);
                        utilService.printJsonResult(true, "操作成功！", "no", response);
                    } else {
                        utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]
}
